package com.hirsi.common.core.util;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * 加密解密工具
 */
@Component
public class EncryptUtil {

    private static String privateKey;

    private static String publicKey;

    public static void main(String[] args) {
        String s = SecureUtil.rsa("MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKG3EGol/i2Rb101oLOBhhUfb31T7diCfgcYMZ55JrhL4tBEBA1D/aTCoi/qaBucREet9Ij4WmOr9zsDjoNiaky00u3ulUrl6yrilko4MlQPl34MQi/1HKDtHaW5m3Ar/GTsCKpuqrU5u7t5Mx9a97y48cMNzVusRrqorZKSQD7HAgMBAAECgYAVBqFKu9/deuBY5SUc0cVmL60mRWhSUz0hnQKukR/4rF00KPPB6corxuahhj+8/0ddbn+x42CVPwGpGaBI8t3RH+N+Tlh4VONSlF+A/ashBc4j3So/oj5s5CwgyuE/1VN6mtkYaGHAUne85wTAzjsW4xD7i47wdKkMkj53J7pEuQJBAOuGXgyDagMSp2oJC2H07PZrXvbWmjMPvufGgdvcbfV6fW7Z3uCi1kuNs606puFXyb4zyap9T9poNEnyYHdzyHMCQQCvxg1CVe+/MdGQGnBIfcuJFEbhzWHjZZEn8ygaIdR/5OY/VRfCuN2SZTWHuOdtCi3cIbrhoXai3byeoerOS59dAkEAwgyW+GBdu+JmpOqGwlJ12iFkwKLXsTJL69KVs3vN7O7a4bYH97SyMtWxMAye6hLWWSt6AEn2vNAWJMbZzBFNGQJAVhlnSUUbCMZ4ZmPs+DVWS7E4/0ZBWsfXkIAU9qf3lSYkeoVN/D3dmuYkkHTMN6cyy49iIT+cAy8mQ3YAtEhqqQJBANUCQ2WKoqMp/zBj6JrV9Tbd8oWEDAMn2j3IcjP9n4G3RK+6/iLp+PZy1qFYZxOb3gs3ZmODGNlLnaMBZak8IFE=", null).encryptBase64(StrUtil.bytes("1", CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
        System.out.println(s);
        /*String s = SecureUtil.rsa(null, "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQChtxBqJf4tkW9dNaCzgYYVH299U+3Ygn4HGDGeeSa4S+LQRAQNQ/2kwqIv6mgbnERHrfSI+Fpjq/c7A46DYmpMtNLt7pVK5esq4pZKODJUD5d+DEIv9Ryg7R2luZtwK/xk7Aiqbqq1Obu7eTMfWve8uPHDDc1brEa6qK2SkkA+xwIDAQAB").decryptStr("bkz7llMQBOOQw8szutA/L70GRbM8tFTq0OQlcnG7GYqMHY7VsY9t7HEG0ak/qI+NjdVOfBjTPeab+B7tqfmRKiOXJdYBs3e+HQdKCrJxAvjO+RM52hLyikg/RKRtsjuk+U2GphOHtLAxLZdT4LJCj35MacmQePVWZgJ7Mnb5MF8=", KeyType.PublicKey);
        System.out.println(s);*/
    }

    @Value("${ztk.private_key}")
    public void setPrivateKey(String privateKey) {
        EncryptUtil.privateKey = privateKey;
    }

    @Value("${ztk.public_key}")
    public void setPublicKey(String publicKey) {
        EncryptUtil.publicKey = publicKey;
    }

    /**
     * 私钥加密（后端加密）
     * @param data
     * @return
     */
    public static String encrypt(String data) {
        if (StrUtil.isBlank(data)) return null;
        return cn.hutool.crypto.SecureUtil.rsa(privateKey, null).encryptBase64(StrUtil.bytes(data, CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
    }

    /**
     * 私钥解密
     * @param encryptData
     * @return
     */
    public static String decrypt(String encryptData) {
        if (StrUtil.isBlank(encryptData)) return null;
        return cn.hutool.crypto.SecureUtil.rsa(privateKey, null).decryptStr(encryptData, KeyType.PrivateKey);
    }

    /**
     * 公钥加密
     * @param data
     * @return
     */
    public static String publicKeyEncrypt(String data) {
        if (StrUtil.isBlank(data)) return null;
        return cn.hutool.crypto.SecureUtil.rsa(null, publicKey).encryptBase64(StrUtil.bytes(data, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
    }

    /**
     * 公钥解密（后端解密）
     * @param encryptData
     * @return
     */
    public static String publicKeyDecrypt(String encryptData) {
        if (StrUtil.isBlank(encryptData)) return null;
        return cn.hutool.crypto.SecureUtil.rsa(null, publicKey).decryptStr(encryptData, KeyType.PublicKey);
    }
}
